<?php

namespace Admin\Model;
use Think\Model;

class OrderModel extends Model{
    // 表单允许提交的字段
    protected $insertFields = array('shr_name','shr_province','shr_city','shr_area','shr_address','shr_tel','pay_method','post_method');
    protected $_validate = array(
        array('shr_name', 'require', '收货人姓名不能为空！', 1, 'regex', 3),
        array('shr_province', 'require', '收货人所在省不能为空！', 1, 'regex', 3),
        array('shr_city', 'require', '收货人城市不能为空！', 1, 'regex', 3),
        array('shr_area', 'require', '收货人地区不能为空！', 1, 'regex', 3),
        array('shr_address', 'require', '收货人地址不能为空！', 1, 'regex', 3),
        array('shr_tel', 'require', '收货人电话不能为空！', 1, 'regex', 3),
        array('pay_method', 'require', '支付方式不能为空！', 1, 'regex', 3),
        array('post_method', 'require', '发货方式不能为空！', 1, 'regex', 3),
    ); 
    
    protected function _before_insert(&$data, $options) {
        //判断购物车是否有商品
        $cartModel = D('Admin/Cart');
        $cartData = $cartModel->cartList();
        if(count($cartData)==0){
            $this->error='必须购买商品才能下单';
            return FALSE;
        }
        //循环购物车中的每件商品 检查库存量够不够 并计算总价
        //加锁   防止高并发时  数据混乱
        $this->fp= fopen('./order.lock', 'r');
        flock($this->fp, LOCK_EX);
        $tp = 0;//总价
        $gnModel = M('GoodsNumber');
        
        $buythis =  session('buythis');
        foreach ($cartData as $k => $v) {
            //判断这件商品有没有被选中
            if(!in_array($v['goods_id'].'-'.$v['goods_attr_id'], $buythis))
                    continue;
            //取出这件商品的库存量
            $gn = $gnModel->field('goods_number')->where(array(
                'goods_id'=>array('eq',$v['goods_id']),
                'goods_attr_id'=>array('eq',$v['goods_attr_id']),
            ))->find();
            if($gn['goods_number'] < $v['goods_number']){
                $this->error = '商品库存量不足无法下单';
                return FALSE;
            }
            // 计算总价
            $tp += $v['price'] * $v['goods_number'];
        }
        //下单前把订单信息补齐
        $data['member_id']=  session('mid');
        $data['addtime']=  time();
        $data['total_price']=$tp;
        
        //启用事物
        mysql_query('start transaction');
    }
    
    protected function _after_insert($data, $options) {
        //处理订单商品表
        //把购物车的数据存到订单商品表中
        $cartModel = D('Admin/Cart');
        $cartData=$cartModel->cartList();
        //循环购物车中的商品 1减少库存量2插入到订单商品表
        $gnModel = M('GoodsNumber');
        $ogModel = M('OrderGoods');
        $buythis =  session('buythis');
        foreach ($cartData as $k => $v) {
            //如果这件商品没有勾选就不处理
            if(!in_array($v['goods_id'].'-'.$v['goods_attr_id'], $buythis))
            continue;
            //减少库存量
            $rs = $gnModel->where(array(
                'goods_id' => array('eq',$v['goods_id']),
                'goods_attr_id' => array('eq',$v['goods_attr_id']),
            ))->setDec('goods_number',$v['goods_number']);
            if($rs ==FALSE){
                mysql_query('rollback');
                return FALSE;
            }
            // 插入到定单商品表
            $rs = $ogModel->add(array(
                'order_id' => $data['id'],
                'member_id' => session('mid'),
                'goods_id' => $v['goods_id'],
                'goods_attr_id' => $v['goods_attr_id'],
                'goods_attr_str' => $v['goods_attr_str'],
                'goods_price' => $v['price'],
                'goods_number' => $v['goods_number'],
            ));
            if($rs === FALSE)
            {
                mysql_query('ROLLBACK');
                return FALSE;
            }
        }
        //提交事务
        mysql_query('commit');
        //释放锁
        flock($this->fp, LOCK_UN);
        fclose($this->fp);
        //清空购物车中所选择的商品
        $cartModel->clearDb();
        //删除session中的所选中的商品
        session('buythis',null);
    }
    //设置订单为已经支付的状态
    public function setPaid($id) {
        //更新订单的状态为已经支付
        $this->where(array('id'=>array('eq',$id)))->setField('pay_status',1);
        // 增加会员的经验值和积分 - 定单的总价是多少就加多少经验值和积分
        $info = $this->field('total_price,member_id')->find($id);
        $this->execute('UPDATE sp_member SET jyz=jyz+'.$info['total_price'],',jifen=jifen+'.$info['total_price'].' WHERE id='.$info['member_id']);

    }
}
